﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using CH.FourBoards.LagerWalti.RichFrontEnd.Framework;
using CH.FourBoards.LagerWalti.Service.DTOs.MasterData;
using CH.FourBoards.LagerWalti.Service.MasterData;

namespace CH.FourBoards.LagerWalti.RichFrontEnd.ViewModel
{
    public class ProductsPresenter : ObservableObject
    {

        private readonly MasterDataService _service = new MasterDataService();
        private string _productName;
        private readonly ObservableCollection<ProductDTO> _products = new ObservableCollection<ProductDTO>();
        private readonly ObservableCollection<string> _productNames = new ObservableCollection<string>();
        
        public ProductsPresenter()
        {
            LoadProducts();
        }

        private void LoadProducts()
        {
            var products = _service.ListProducts().Products;
            _products.Clear();
            _productNames.Clear();
            foreach (var product in products) {
                _products.Add(product);
                _productNames.Add(product.Name);
            }
        }

        public string ProductName
        {
            get { return _productName; }
            set
            {
                _productName = value;
                RaisePropertyChangedEvent("ProductName");
            }
        }

        public IEnumerable<ProductDTO> Products
        {
            get { return _products; }
        }

        public IEnumerable<string> ProductNames
        {
            get { return _productNames; }
        }

        public ICommand AddProductCommand
        {
            get { return new DelegateCommand(AddProduct); }
        }

        private void AddProduct()
        {
            var product = new ProductDTO {
                Name = ProductName,
                BundleSize = 1,
                CategoryId = 1
            };
            _service.AddProduct(product);
            LoadProducts();
        }
    }
}
